package games.mapacman.client;

import games.mapacman.common.consts;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;




public class MaPacmanGui extends JFrame{
	
private static MaPacman client;
private JTextField sendText;
private JPanel panel;
private LoginPanel loginpanel;
public GameScreen screen;

public MaPacmanGui(MaPacman machatclient)
{
	client=machatclient;
	loginpanel = new LoginPanel(client);
	this.setSize(600,450);
	this.setTitle("JMaPacMan "+consts.VERSION);
	this.setLocationRelativeTo(null);
	addWindowListener(new WindowAdapter() {
		  public void windowClosing(WindowEvent e) {
			  client.close();
		  }
		});

	this.setLayout(new BorderLayout(1,2));	
	this.add(loginpanel,BorderLayout.CENTER);
	
	
	
	
	 addWindowListener(new WindowAdapter() 
		      {
		      public void windowClosing(WindowEvent e) 
		        {
		          client.stopGame();
		      }
		      });

}

public GameScreen prepareScreen()
{
	//this.remove(loginpanel);
	panel = new JPanel();
	sendText = new JTextField("",25);
	sendText.addKeyListener(new PanelKeyListener(client));
	ActionListener sendclick = new ActionListener()
	{
		public void actionPerformed( ActionEvent e )
		{
			client.sendChatMsg(sendText.getText());
			sendText.setText("");
		}
	};
	sendText.addActionListener(sendclick);
	this.add(panel,BorderLayout.CENTER);
	this.add(sendText,BorderLayout.SOUTH);
	this.validate();
	sendText.requestFocus();

	panel.setLayout(null);
    
	// setup our canvas size and put it into the content of the frame
	Canvas canvas=new Canvas();
	canvas.setBounds(0,0,600,400);
    // Tell AWT not to bother repainting our canvas since we're
    // going to do that our self in accelerated mode
    canvas.setIgnoreRepaint(true);
    panel.add(canvas);
	BufferStrategy strategy;
    canvas.createBufferStrategy(2);
    strategy = canvas.getBufferStrategy();
      
	canvas.addFocusListener(new FocusListener()
      {
      public void focusGained(FocusEvent e)
        {
        sendText.requestFocus();
        }
            
      public void focusLost(FocusEvent e)
        {
        }
      });        

	
    GameScreen.createScreen(strategy,600,355,64,64,panel,client.getMyPlayerName());
    screen=GameScreen.get();
	
	return screen;
}

public void printText(String text) {
	if (screen!=null)
		screen.printChatText(text);
	else
	{
		System.out.println(text);
	}
}

public void ErrorMsg(String text)
{
	JOptionPane.showMessageDialog(this, text, "Error", JOptionPane.WARNING_MESSAGE);
}

public void init()
{
	loginpanel.init();
}

public GameScreen getScreen() {
	return screen;
}


	
}
